/*
 * @author Guillermo Adrian Salguero Ortega
 */
package formula_urjc;

import clases.Carrera;
import clases.Mundial;
import java.util.ArrayList;
import javax.swing.table.DefaultTableModel;

public class MundialLanza extends javax.swing.JFrame {

    /**
     * Creates new form AltaEscuderia
     */
   DefaultTableModel dtmEjemplo;
   private Mundial mundial;
    public MundialLanza(Mundial mundial) {
        initComponents();
        //prevervo la instancia del mundial
        this.mundial= mundial;
        //Llenamos el modelo
        dtmEjemplo = new DefaultTableModel(null,getColumnas());
 
        setFilas();
 
        this.tablaCarreras.setModel(dtmEjemplo);
        this.setSize(500, 500);
        setDefaultCloseOperation(javax.swing.WindowConstants.DISPOSE_ON_CLOSE);
    
    }
     private ArrayList<Carrera> listaCarreras;
    //Encabezados de la tabla
    private String[] getColumnas()
    {
          String columna[] = new String[]{"Nombre","Circuito","Realizada"};
          return columna;
    }
 
    private void setFilas()
    {
      try{
        this.listaCarreras = mundial.getListaCarreras();
        if (!(listaCarreras.isEmpty())){
         // Recorro la lista ocn un foreach
            for(Carrera carreraAux : listaCarreras) {
             // Recupero las variables
                String nombre = carreraAux.getNombre();
                String circuito = carreraAux.getCircuito();
                boolean realizada = carreraAux.isRealizada();
                String realizadaStr = "No";  
                if(realizada){realizadaStr="Si";}       
                Object objeto[]={nombre, circuito, realizadaStr };
                dtmEjemplo.addRow(objeto);
            }
        }
       }catch (NullPointerException ex){
            System.out.println("Listado de circuitos vacio");
       }
       
    }

    /**
     * This method is called from within the constructor to initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is always
     * regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jPanel1 = new javax.swing.JPanel();
        jLabel1 = new javax.swing.JLabel();
        jScrollPane1 = new javax.swing.JScrollPane();
        tablaCarreras = new javax.swing.JTable();
        jButton2 = new javax.swing.JButton();
        jButton1 = new javax.swing.JButton();

        setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);

        jLabel1.setText("Correr Mundial");

        tablaCarreras.setModel(new javax.swing.table.DefaultTableModel(
            new Object [][] {
                {null, null, null},
                {null, null, null},
                {null, null, null},
                {null, null, null}
            },
            new String [] {
                "Nombre", "Circuito", "Realizada"
            }
        ) {
            Class[] types = new Class [] {
                java.lang.String.class, java.lang.String.class, java.lang.Boolean.class
            };
            boolean[] canEdit = new boolean [] {
                false, false, false
            };

            public Class getColumnClass(int columnIndex) {
                return types [columnIndex];
            }

            public boolean isCellEditable(int rowIndex, int columnIndex) {
                return canEdit [columnIndex];
            }
        });
        tablaCarreras.addMouseListener(new java.awt.event.MouseAdapter() {
            public void mouseClicked(java.awt.event.MouseEvent evt) {
                tablaCarrerasseleccionarItem(evt);
            }
        });
        jScrollPane1.setViewportView(tablaCarreras);

        jButton2.setText("Comenzar Carrera");
        jButton2.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButton2ActionPerformed(evt);
            }
        });

        jButton1.setText("Ver Clasificacion");
        jButton1.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButton1ActionPerformed(evt);
            }
        });

        javax.swing.GroupLayout jPanel1Layout = new javax.swing.GroupLayout(jPanel1);
        jPanel1.setLayout(jPanel1Layout);
        jPanel1Layout.setHorizontalGroup(
            jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel1Layout.createSequentialGroup()
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(jPanel1Layout.createSequentialGroup()
                        .addGap(23, 23, 23)
                        .addComponent(jLabel1))
                    .addGroup(jPanel1Layout.createSequentialGroup()
                        .addContainerGap()
                        .addComponent(jScrollPane1, javax.swing.GroupLayout.PREFERRED_SIZE, 359, javax.swing.GroupLayout.PREFERRED_SIZE))
                    .addGroup(jPanel1Layout.createSequentialGroup()
                        .addGap(78, 78, 78)
                        .addComponent(jButton2)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                        .addComponent(jButton1)))
                .addContainerGap(192, Short.MAX_VALUE))
        );
        jPanel1Layout.setVerticalGroup(
            jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel1Layout.createSequentialGroup()
                .addGap(24, 24, 24)
                .addComponent(jLabel1)
                .addGap(26, 26, 26)
                .addComponent(jScrollPane1, javax.swing.GroupLayout.PREFERRED_SIZE, 167, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addGap(18, 18, 18)
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jButton2)
                    .addComponent(jButton1))
                .addContainerGap(52, Short.MAX_VALUE))
        );

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(jPanel1, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                .addContainerGap())
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(jPanel1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
        );

        pack();
    }// </editor-fold>//GEN-END:initComponents

    private void tablaCarrerasseleccionarItem(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_tablaCarrerasseleccionarItem

    }//GEN-LAST:event_tablaCarrerasseleccionarItem

    private void jButton2ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButton2ActionPerformed
       
       int row = this.tablaCarreras.getSelectedRow();
      if (row>=0){  
        if(!(mundial.getListaCarreras().get(row).realizada)){
                // Corro la carrera
                mundial.getListaCarreras().get(row).rellenarClasificacion();
                // Muestro los resultados de la carrera
                ResultadoCarrera resultCarr = new ResultadoCarrera(mundial, row);
                this.setVisible(false);
                resultCarr.setVisible(true);
        }else{// Si ya se ha corrido la carrera
        javax.swing.JOptionPane. showMessageDialog (this, "La carrera ya ha sido corrida");
        }
       }else{// Si no ha elegido carrera
        javax.swing.JOptionPane. showMessageDialog (this, "Elija una carrera de la tabla");
        }
        
    }//GEN-LAST:event_jButton2ActionPerformed

    private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButton1ActionPerformed
       int row = this.tablaCarreras.getSelectedRow();
       if (row>=0){  
        if(mundial.getListaCarreras().get(row).realizada){
            // Muestro los resultados de la carrera
             ResultadoCarrera resultCarr = new ResultadoCarrera(mundial, row);
             this.setVisible(false);
              resultCarr.setVisible(true);
        }else{// Si todavia no se ha corrido la carrera
        javax.swing.JOptionPane. showMessageDialog (this, "La carrera todavia no se ha corrido");
    }//GEN-LAST:event_jButton1ActionPerformed
    }else{// Si no ha elegido carrera
            javax.swing.JOptionPane. showMessageDialog (this, "Elija una carrera de la tabla");
        }
     } 
    

    
    /**
     * @param args the command line arguments
     */
    public static void main(String args[]) {
        /* Set the Nimbus look and feel */
        //<editor-fold defaultstate="collapsed" desc=" Look and feel setting code (optional) ">
        /* If Nimbus (introduced in Java SE 6) is not available, stay with the default look and feel.
         * For details see http://download.oracle.com/javase/tutorial/uiswing/lookandfeel/plaf.html 
         */
        try {
            for (javax.swing.UIManager.LookAndFeelInfo info : javax.swing.UIManager.getInstalledLookAndFeels()) {
                if ("Nimbus".equals(info.getName())) {
                    javax.swing.UIManager.setLookAndFeel(info.getClassName());
                    break;
                }
            }
        } catch (ClassNotFoundException ex) {
            java.util.logging.Logger.getLogger(AltaEscuderia.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (InstantiationException ex) {
            java.util.logging.Logger.getLogger(AltaEscuderia.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (IllegalAccessException ex) {
            java.util.logging.Logger.getLogger(AltaEscuderia.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (javax.swing.UnsupportedLookAndFeelException ex) {
            java.util.logging.Logger.getLogger(AltaEscuderia.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        }
        //</editor-fold>

        /* Create and display the form */
        java.awt.EventQueue.invokeLater(new Runnable() {
            public void run() {
                new MundialForm().setVisible(true);
            }
        });
    }

    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton jButton1;
    private javax.swing.JButton jButton2;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JPanel jPanel1;
    private javax.swing.JScrollPane jScrollPane1;
    private javax.swing.JTable tablaCarreras;
    // End of variables declaration//GEN-END:variables
}
